#include "randomthread.h"
#include <QThread>
#include <QDebug>

RandomThread::RandomThread(QThread *parent):QThread(parent)
{

}

void RandomThread::run()
{

}

SortThread::SortThread(QThread *parent):QThread(parent)
{

}

void SortThread::run()
{

}

void ThreadObj::Generate()
{
    qDebug()<<"Generate"<<QThread::currentThreadId() <<endl;
    std::vector<int> nums;
    for(int i = 0; i <100; i++)
    {
        nums.push_back(rand() % 1000);
    }
    emit GenerateFinished(nums);        // 手动发出信号
}

void ThreadObj::Sort()
{
    qDebug()<<"Sort"<<QThread::currentThreadId() <<endl;
    for(int i = 0; i < nums.size() -1; i++)
    {
        for(int j = 0; j < nums.size() - i - 1; j++)
        {
            if(nums[j] > nums[j+1])
            {
                std::swap(nums[j], nums[j+1]);
            }
        }
    }
    emit SortFinished(nums);
}

void RandomRunnable::run()
{
    qDebug()<<"Generate"<<QThread::currentThreadId() <<endl;
    std::vector<int> nums;
    for(int i = 0; i <5000; i++)
    {
        nums.push_back(rand() % 5000);
    }
    emit GenerateFinished(nums);        // 手动发出信号
}

void SortRunnable::run()
{
    qDebug()<<"Sort"<<QThread::currentThreadId() <<endl;
    for(int i = 0; i < nums.size() -1; i++)
    {
        for(int j = 0; j < nums.size() - i - 1; j++)
        {
            if(nums[j] > nums[j+1])
            {
                std::swap(nums[j], nums[j+1]);
            }
        }
    }
    emit SortFinished(nums);
}
